package spt.slides;

import java.util.List;
import java.util.logging.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import spt.utils.XMLUtils;

public class SlideXMLWriter {
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(SlideXMLWriter.class.getName());
	private static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	
	@Deprecated
	public static String write2(Slide slide){
		Document document = XMLUtils.newDocument();
		Element slideElement = document.createElement("slide");
		document.appendChild(slideElement);
		
		slideElement.setAttribute("id", Integer.toString(slide.getId()));
		slideElement.setTextContent(slide.getContent());
		if(slide.getPublishStart() != null)
			slideElement.setAttribute("publish-start", slide.getPublishStart().toString());
		
		if(slide.getPublishStop() != null)
			slideElement.setAttribute("publish-stop", slide.getPublishStop().toString());
		
		slideElement.setAttribute("display-time", Integer.toString(slide.getDisplayTime()));
		slideElement.setAttribute("always-published", Boolean.toString(slide.isAlwaysPublished()));
		
		
		
		return XMLUtils.transformToString(document);
	}
	
	public static String write(Slide slide){
		StringBuffer xml = new StringBuffer();
		xml.append(HEADER + "\n");
		xml.append("<slide");
		xml.append(" id=\"" + slide.getId() + "\"");
		if(slide.getPublishStart() != null)
			xml.append(" publish-start=\"" + slide.getPublishStart() + "\"");
		if(slide.getPublishStop() != null)
			xml.append(" publish-stop=\"" + slide.getPublishStop() + "\"");
		xml.append(" display-time=\"" + slide.getDisplayTime() + "\"");
		xml.append(" always-published=\"" + slide.isAlwaysPublished() + "\"");
		xml.append(">\n");
		xml.append(slide.getContent());
		xml.append("</slide>");
		
		return xml.toString();
	}
	
	@Deprecated
	public static String writeList2(List<Slide> slides){
		Document document = XMLUtils.newDocument();
		Element content = document.createElement("content");
		document.appendChild(content);
		
		for(Slide slide : slides){
			Element slideElement = document.createElement("slide");
			slideElement.setAttribute("display-time", Integer.toString(slide.getDisplayTime()));
			slideElement.setTextContent(slide.getContent());
			content.appendChild(slideElement);
		}
		
		return XMLUtils.transformToString(document);
	}
	
	public static String writeList(List<Slide> slides){
		StringBuffer xml = new StringBuffer();
		xml.append(HEADER + "\n");
		xml.append("<content>\n");
		for(Slide slide : slides){
			xml.append("<slide");
			xml.append(" display-time=\"" + slide.getDisplayTime() + "\"");
			xml.append(">\n");
			xml.append(slide.getContent());
			xml.append("</slide>\n");
		}
		xml.append("</content>");
		return xml.toString();
	}
	
	public static String writeIdsList(List<Integer> ids){
		if(ids.isEmpty())
			return HEADER + "\n<slides />";
		
		StringBuffer xml = new StringBuffer();
		xml.append(HEADER + "\n");
		xml.append("<slides>\n");
		for(Integer id : ids){
			xml.append("<slide");
			xml.append(" id=\"" + id + "\"");
			xml.append(">\n");
		}
		xml.append("</slides>");
		return xml.toString();
	}
}
